package com.tianqingwl.gsystem.content.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianqingwl.gsystem.common.vo.CommonResultVo;
import com.tianqingwl.gsystem.common.vo.PageResultVo;
import com.tianqingwl.gsystem.content.param.ContentInfoQueryParam;
import com.tianqingwl.gsystem.content.pojo.ContentInfo;
import com.tianqingwl.gsystem.content.pojo.ContentType;
import com.tianqingwl.gsystem.content.service.ContentInfoService;
import com.tianqingwl.gsystem.content.service.ContentTypeService;
import com.tianqingwl.gsystem.resources.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/manage/content")
@PreAuthorize("@gs.check('content_manage')")
public class ContentController {

    @Autowired
    private ContentTypeService contentTypeService;

    @Autowired
    private ContentInfoService contentInfoService;

    @Autowired
    private SysConfigService sysConfigService;


    @GetMapping("/content-manage.html")
    public String contentManagePage(Model model) {
        List<ContentType> contentTypeList = contentTypeService.listAllType();
        model.addAttribute("contentTypeList", contentTypeList);
        return "manage/content/content-manage";
    }

    @GetMapping("/content-file-resources-manage.html")
    public String contentImagesManage(Model model) {

        return "manage/content/content-file-resources-manage";
    }

    @GetMapping("/content-edit.html")
    public String contentCreatePage(Model model, Long contentId) {
        if (contentId != null) {
            ContentInfo contentInfo = contentInfoService.getContentInfoById(contentId);
            model.addAttribute("contentInfo", contentInfo);
        }
        List<ContentType> contentTypeList = contentTypeService.listAllType();
        model.addAttribute("contentTypeList", contentTypeList);
        return "manage/content/content-edit";
    }

    @PostMapping("/save")
    @ResponseBody
    public Object saveContent(ContentInfo contentInfo, String baiduSubmit) {
        if (contentInfo == null) {
            return CommonResultVo.fail("no data");
        }
        try {
            contentInfoService.save(contentInfo);
            //百度链接提交
            if (!StringUtils.isEmpty(baiduSubmit) && "1".equals(baiduSubmit)) {
                CommonResultVo commonResultVo = contentInfoService
                        .baiduSubmit(contentInfo.getContentId(), null);
            }
            return CommonResultVo.success(contentInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResultVo.fail(e.getMessage());
        }
    }

    @PostMapping("/baiduSubmit")
    @ResponseBody
    public Object baiduSubmit(Long[] contentIds) {
        if (contentIds == null || contentIds.length == 0) {
            return CommonResultVo.fail("没有选择文章");
        }
        return contentInfoService.baiduSubmit(null, contentIds);
    }

    @GetMapping("/delete")
    @ResponseBody
    public Object delete(Long contentId, Long[] contentIds) {
        if (contentId != null) {
            contentInfoService.deleteById(contentId);
            return CommonResultVo.success();
        }
        if (contentIds != null && contentIds.length > 0) {
            contentInfoService.deleteByIdList(Arrays.asList(contentIds));
            return CommonResultVo.success();
        }
        return CommonResultVo.fail("no data");
    }


    @GetMapping("/content-tag-manage.html")
    public String contentTagPage() {
        return "manage/content/content-tag-manage";
    }

    @GetMapping("/list")
    @ResponseBody
    public Object list(Long page, Long limit, ContentInfoQueryParam queryParam, Integer searchType, String searchStr) {
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        Page<ContentInfo> contentPage = new Page<>(page, limit);
        //搜索类型对搜索参数封装
        if (searchType != null && !StringUtils.isEmpty(searchStr)) {
            switch (searchType) {
                case 1:
                    queryParam.setContentTitle(searchStr);
                    break;
                case 2:
                    queryParam.setUserShowName(searchStr);
                    break;
            }
        }
        contentInfoService.list(contentPage, queryParam);
        return PageResultVo.success(contentPage.getTotal(), contentPage.getPages(), contentPage.getRecords());
    }

    @GetMapping("/test.html")
    public String testHtml(){
        return "manage/test";
    }

}
